home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / grapdrvs / x11draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  15.6 KB  |  360 lines

  1. /*****************************************************************************
  2. *   An X11 drawing routines.                             *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, June 1993.  *
  5. *****************************************************************************/
  6.  
  7. #ifdef __hpux
  8. typedef char *caddr_t;           /* Awful kludge. Let me know of a better way. */
  9. #endif /* __hpux */
  10.  
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/Xresource.h>
  15.  
  16. #include "irit_sm.h"
  17. #include "genmat.h"
  18. #include "iritprsr.h"
  19. #include "iritgrap.h"
  20. #include "attribut.h"
  21. #include "x11drvs.h"
  22.  
  23. #define X11_MAP_X_COORD(x) (((int) ((x + 1.0) * ViewWidth)) / 2)
  24. #define X11_MAP_Y_COORD(y) (((int) ((1.0 - y) * ViewHeight)) / 2)
  25.  
  26. static int
  27.     CurrentXPosition = 0,
  28.     CurrentYPosition = 0;
  29. static XGCValues CrntColorHighIntensity, CrntColorLowIntensity;
  30.  
  31. static void SetColorIndex(int color);
  32. static void SetColorRGB(int Color[3]);
  33.  
  34. /*****************************************************************************
  35. * DESCRIPTION:                                                               M
  36. * Sets up a view window.                             M
  37. *                                                                            *
  38. * PARAMETERS:                                                                M
  39. *   argc, argv:   Command line.                             M
  40. *                                                                            *
  41. * RETURN VALUE:                                                              M
  42. *   void                                                                     M
  43. *                                                                            *
  44. * KEYWORDS:                                                                  M
  45. *   SetViewWindow                                                            M
  46. *****************************************************************************/
  47. void SetViewWindow(int argc, char **argv)
  48. {
  49.     Cursor XCursor;
  50.     long ValueMask;
  51.     XSizeHints Hints;
  52.     XSetWindowAttributes SetWinAttr;
  53.  
  54.     SetWinAttr.background_pixel = ViewBackGroundPixel;
  55.     SetWinAttr.border_pixel = ViewBorderPixel;
  56.     ValueMask = CWBackPixel | CWBorderPixel;
  57.  
  58.     Hints.flags = PMinSize | PMaxSize;
  59.     Hints.x = Hints.y = 1;
  60.     Hints.min_width = 50;
  61.     Hints.max_width = 2000;
  62.     Hints.min_height = 50;
  63.     Hints.max_height = 2000;
  64.     if (ViewHasSize) {
  65.     Hints.flags |= PSize;
  66.     if (ViewWidth < Hints.min_width)
  67.         ViewWidth = Hints.min_width;
  68.     if (ViewWidth > Hints.max_width)
  69.         ViewWidth = Hints.max_width;
  70.     if (ViewHeight < Hints.min_height)
  71.         ViewHeight = Hints.min_height;
  72.     if (ViewHeight > Hints.max_height)
  73.         ViewHeight = Hints.max_height;
  74.     Hints.width = ViewWidth;
  75.     Hints.height = ViewHeight;
  76.     }
  77.     else {
  78.     Hints.flags |= PSize;
  79.     Hints.width = ViewWidth = DEFAULT_VIEW_WIDTH;
  80.     Hints.height = ViewHeight = DEFAULT_VIEW_HEIGHT;
  81.     }
  82.     if (ViewHasPos) {
  83.     Hints.flags |= USPosition;
  84.     Hints.x = ViewPosX;
  85.     Hints.y = ViewPosY;
  86.     }
  87.  
  88.     ViewWndw = XCreateWindow(XDisplay, XRoot,
  89.                  ViewPosX, ViewPosY,
  90.                  ViewWidth, ViewHeight,
  91.                  1, 0, CopyFromParent, CopyFromParent,
  92.                  ValueMask, &SetWinAttr);
  93.  
  94.     XSetStandardProperties(XDisplay, ViewWndw,
  95.                RESOURCE_NAME, RESOURCE_NAME, None,
  96.                argv, argc,
  97.                &Hints);
  98.  
  99.     /* Set our own cursor: */
  100.     XCursor = XCreateFontCursor(XDisplay, XC_iron_cross);
  101.     XDefineCursor(XDisplay, ViewWndw, XCursor);
  102.     if (ViewCursorColor != NULL)
  103.     XRecolorCursor(XDisplay, XCursor, ViewCursorColor, &BlackColor);
  104.  
  105.     XSelectInput(XDisplay, ViewWndw, ExposureMask | ButtonPressMask);
  106.     
  107.     XMapWindow(XDisplay, ViewWndw);
  108. }
  109.  
  110. /*****************************************************************************
  111. * DESCRIPTION:                                                               M
  112. * Redraws the view window.                             M
  113. *                                                                            *
  114. * PARAMETERS:                                                                M
  115. *   None                                                                     M
  116. *                                                                            *
  117. * RETURN VALUE:                                                              M
  118. *   void                                                                     M
  119. *                                                                            *
  120. * KEYWORDS:                                                                  M
  121. *   IGRedrawViewWindow                                                       M
  122. *****************************************************************************/
  123. void IGRedrawViewWindow(void)
  124. {
  125.     IPObjectStruct *PObj, *MatObj;
  126.     MatrixType CrntViewMat;
  127.  
  128.     XClearWindow(XDisplay, ViewWndw);
  129.  
  130.     switch (IGGlblViewMode) {         /* Update the current view. */
  131.     case IG_VIEW_ORTHOGRAPHIC:
  132.         GEN_COPY(CrntViewMat, IritPrsrViewMat, sizeof(MatrixType));
  133.         break;
  134.     case IG_VIEW_PERSPECTIVE:
  135.         MatMultTwo4by4(CrntViewMat, IritPrsrViewMat, IritPrsrPrspMat);
  136.         break;
  137.     }
  138.  
  139.     for (PObj = IGGlblDisplayList; PObj != NULL; PObj = PObj -> Pnext) {
  140.     int Visible = TRUE;
  141.  
  142.     if ((MatObj = AttrGetObjectObjAttrib(PObj, "_animation_mat")) != NULL &&
  143.         IP_IS_MAT_OBJ(MatObj)) {
  144.         if (Visible = AttrGetObjectIntAttrib(PObj, "_isvisible")) {
  145.         MatMultTwo4by4(IGGlblCrntViewMat,
  146.                    *MatObj -> U.Mat, IritPrsrViewMat);
  147.         if (IGGlblViewMode == IG_VIEW_PERSPECTIVE) 
  148.             MatMultTwo4by4(IGGlblCrntViewMat,
  149.                    IGGlblCrntViewMat, IritPrsrPrspMat);
  150.         }
  151.     }
  152.     else
  153.         GEN_COPY(IGGlblCrntViewMat, CrntViewMat, sizeof(MatrixType));
  154.  
  155.     if (Visible)
  156.         IGDrawObject(PObj);
  157.     }
  158.  
  159.     XFlush(XDisplay);
  160. }
  161.  
  162. /*****************************************************************************
  163. * DESCRIPTION:                                                               M
  164. * To handle internal events. Should not block.                               M
  165. *                                                                            *
  166. * PARAMETERS:                                                                M
  167. *   None                                                                     M
  168. *                                                                            *
  169. * RETURN VALUE:                                                              M
  170. *   void                                                                     M
  171. *****************************************************************************/
  172. void IGHandleInternalEvents(void)
  173. {
  174. }
  175.  
  176. /*****************************************************************************
  177. * DESCRIPTION:                                                               M
  178. * Low level 2D drawing routine. Coordinates are normalized to -1 to 1 by     M
  179. * this time.                                                                 M
  180. *                                                                            *
  181. * PARAMETERS:                                                                M
  182. *   X, Y:    Coordinates of 2D location to move to.                          M
  183. *                                                                            *
  184. * RETURN VALUE:                                                              M
  185. *   void                                                                     M
  186. *                                                                            *
  187. * KEYWORDS:                                                                  M
  188. *   IGMoveTo2D                                                               M
  189. *****************************************************************************/
  190. void IGMoveTo2D(RealType X, RealType Y)
  191. {
  192.     CurrentXPosition = X11_MAP_X_COORD(X);
  193.     CurrentYPosition = X11_MAP_Y_COORD(Y);
  194. }
  195.  
  196. /*****************************************************************************
  197. * DESCRIPTION:                                                               M
  198. * Low level 2D drawing routine. Coordinates are normalized to -1 to 1 by     M
  199. * this time.                                                                 M
  200. *                                                                            *
  201. * PARAMETERS:                                                                M
  202. *   X, Y:    Coordinates of 2D location to draw to.                          M
  203. *                                                                            *
  204. * RETURN VALUE:                                                              M
  205. *   void                                                                     M
  206. *                                                                            *
  207. * KEYWORDS:                                                                  M
  208. *   IGLineTo2D                                                               M
  209. *****************************************************************************/
  210. void IGLineTo2D(RealType X, RealType Y)
  211. {
  212.     int NewX, NewY;
  213.  
  214.     XDrawLine(XDisplay, ViewWndw, XViewGraphContext,
  215.           CurrentXPosition,
  216.           CurrentYPosition,
  217.           NewX = X11_MAP_X_COORD(X),
  218.           NewY = X11_MAP_Y_COORD(Y));
  219.  
  220.     CurrentXPosition = NewX;
  221.     CurrentYPosition = NewY;
  222. }
  223.  
  224. /*****************************************************************************
  225. * DESCRIPTION:                                                               M
  226. * Sets the intensity of a color (high or low).                     M
  227. *                                                                            *
  228. * PARAMETERS:                                                                M
  229. *   High:     TRUE for high, FALSE for low.                                  M
  230. *                                                                            *
  231. * RETURN VALUE:                                                              M
  232. *   void                                                                     M
  233. *                                                                            *
  234. * KEYWORDS:                                                                  M
  235. *   IGSetColorIntensity                                                      M
  236. *****************************************************************************/
  237. void IGSetColorIntensity(int High)
  238. {
  239.     XChangeGC(XDisplay, XViewGraphContext, GCForeground,
  240.           High ? &CrntColorHighIntensity : &CrntColorLowIntensity);
  241.     IGGlblIntensityHighState = High;
  242. }
  243.  
  244. /*****************************************************************************
  245. * DESCRIPTION:                                                               M
  246. * Sets the color of an object according to its color/rgb attributes.         M
  247. *   If object has an RGB attribute it will be used. Otherwise, if the object M
  248. * has a COLOR attribute it will use. Otherwise, WHITE will be used.         M
  249. *                                                                            *
  250. * PARAMETERS:                                                                M
  251. *   PObj:      To set the drawing color to its color.                        M
  252. *                                                                            *
  253. * RETURN VALUE:                                                              M
  254. *   void                                                                     M
  255. *                                                                            *
  256. * KEYWORDS:                                                                  M
  257. *   IGSetColorObj                                                            M
  258. *****************************************************************************/
  259. void IGSetColorObj(IPObjectStruct *PObj)
  260. {
  261.     int c, Color[3];
  262.  
  263.     if (AttrGetObjectRGBColor(PObj, &Color[0], &Color[1], &Color[2])) {
  264.     SetColorRGB(Color);
  265.     }
  266.     else if ((c = AttrGetObjectColor(PObj)) != IP_ATTR_NO_COLOR) {
  267.     SetColorIndex(c);
  268.     }
  269.     else {
  270.     /* Use white as default color: */
  271.     SetColorIndex(IG_IRIT_WHITE);
  272.     }
  273. }
  274.  
  275. /*****************************************************************************
  276. * DESCRIPTION:                                                               M
  277. * Sets the line width to draw the given object, in pixels.             M
  278. *                                                                            *
  279. * PARAMETERS:                                                                M
  280. *   Width:    In pixels of lines to draw with.                               M
  281. *                                                                            *
  282. * RETURN VALUE:                                                              M
  283. *   void                                                                     M
  284. *                                                                            *
  285. * KEYWORDS:                                                                  M
  286. *   IGSetWidthObj                                                            M
  287. *****************************************************************************/
  288. void IGSetWidthObj(int Width)
  289. {
  290.     XGCValues values;
  291.  
  292.     values.line_width = Width;
  293.     XChangeGC(XDisplay, XViewGraphContext, GCLineWidth, &values);
  294. }
  295.  
  296. /*****************************************************************************
  297. * DESCRIPTION:                                                               *
  298. * Sets the color according to the given color index.                     *
  299. *                                                                            *
  300. * PARAMETERS:                                                                *
  301. *   color:     Index of color to use. Must be between 0 and IG_MAX_COLOR.    *
  302. *                                                                            *
  303. * RETURN VALUE:                                                              *
  304. *   void                                                                     *
  305. *****************************************************************************/
  306. static void SetColorIndex(int color)
  307. {
  308.     if (color >= MaxColors)
  309.     color = IG_IRIT_WHITE;
  310.  
  311.     CrntColorHighIntensity.foreground = XViewColorsHigh[color].pixel;
  312.     CrntColorLowIntensity.foreground = XViewColorsLow[color].pixel;
  313.     XChangeGC(XDisplay, XViewGraphContext, GCForeground,
  314.           &CrntColorHighIntensity);
  315.     IGGlblIntensityHighState = TRUE;
  316. }
  317.  
  318. /*****************************************************************************
  319. * DESCRIPTION:                                                               *
  320. * Sets the color according to the given RGB values.                 *
  321. *                                                                            *
  322. * PARAMETERS:                                                                *
  323. *   Color:      An RGB vector of integer values between 0 and 255.           *
  324. *                                                                            *
  325. * RETURN VALUE:                                                              *
  326. *   void                                                                     *
  327. *****************************************************************************/
  328. static void SetColorRGB(int Color[3])
  329. {
  330.     XColor XClr;
  331.  
  332.     XClr.red   = (Color[0] << 8);
  333.     XClr.green = (Color[1] << 8);
  334.     XClr.blue  = (Color[2] << 8);
  335.  
  336.     /* If fails to allocate the color - take WHITE instead. */
  337.     if (!XAllocColor(XDisplay, XColorMap, &XClr)) {
  338.     fprintf(stderr,
  339.         "x11drvs: Failed to allocate color, selected WHITE instead\n");
  340.     XClr.pixel = WhitePixel(XDisplay, XScreen);
  341.     }
  342.     CrntColorHighIntensity.foreground = XClr.pixel;
  343.  
  344.     XClr.red   = (Color[0] << 7);
  345.     XClr.green = (Color[1] << 7);
  346.     XClr.blue  = (Color[2] << 7);
  347.  
  348.     /* If fails to allocate the color - take WHITE instead. */
  349.     if (!XAllocColor(XDisplay, XColorMap, &XClr)) {
  350.     fprintf(stderr,
  351.         "x11drvs: Failed to allocate color, selected WHITE instead\n");
  352.     XClr.pixel = WhitePixel(XDisplay, XScreen);
  353.     }
  354.     CrntColorLowIntensity.foreground = XClr.pixel;
  355.  
  356.     XChangeGC(XDisplay, XViewGraphContext, GCForeground,
  357.           &CrntColorHighIntensity);
  358.     IGGlblIntensityHighState = TRUE;
  359. }
  360.